package com.oocl.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.aspectj.weaver.ast.Or;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.oocl.dao.ClientsDao;
import com.oocl.dao.DeliverInfoDao;
import com.oocl.dao.MerchantsDao;
import com.oocl.dao.OrdersDao;
import com.oocl.dao.base.BaseDao;
import com.oocl.entity.Clients;
import com.oocl.entity.DeliverInfo;
import com.oocl.entity.Foods;
import com.oocl.entity.Merchants;
import com.oocl.entity.Orders;
import com.oocl.entity.SessionInfo;
import com.oocl.service.OrdersService;
import com.oocl.vo.OrdersVo;

@Service("ordersService")
public class OrdersServiceImpl implements OrdersService {
	
	@Resource(name="baseDao")
	private BaseDao baseDao;
	
	@Resource(name="ordersDao")
	private OrdersDao ordersDao;
	
	@Resource(name="merchantsDao")
	private MerchantsDao merchantsDao;
	

	/*
	 * @Method:addOrderList
	 * @Function:insert order into db
	 * @param:DeliverInfo deliverInfo, String icnum,String cid, List<SessionInfo> flist
	 * @author:wendy
	 * @time:2017-08-13
	 * 
	 */
	@Override
	@Transactional
	public String addOrderList(DeliverInfo deliverInfo, String icnum,String cid, List<SessionInfo> flist) {
		// TODO Auto-generated method stub
		System.out.println("配送地址："+deliverInfo.getDid());
		System.out.println("商家："+icnum);
		System.out.println("用户："+cid);
		System.out.println(flist.size());
		
		Merchants m=new Merchants();
		m=merchantsDao.findmerchantsByIcnum(icnum);
		System.out.println("商家名："+m.getMname());
		String oid=UUID.randomUUID().toString();
		List<Orders> olist=new ArrayList<Orders>();
		Clients c=new Clients();
		c=(Clients)baseDao.find(Clients.class, cid);
		System.out.println("user:"+c.getCpwd());
		
		double sum=0;
		for(SessionInfo sesinfo:flist){
			sum+=sesinfo.getFood().getFprice()*sesinfo.getCount();
		}
		
		for(SessionInfo sesinfo:flist){
			Orders o=new Orders();
			System.out.println("jinlai");
			Foods f=new Foods();
			f=sesinfo.getFood();
			
			//初始投诉状态是0（可投诉状态）
			o.setCompstate("0");
			o.setMerchant(m);
			o.setOid(oid);
			o.setFood(f);
			o.setClient(c);
			o.setDeliverInfo(deliverInfo);
			//初始订单状态为0（商家待接单状态）
			o.setOstate("0");
			o.setFcnt(sesinfo.getCount());
			o.setOtime(new Date());
			o.setOsum(sum);
			
			baseDao.persist(o);
			olist.add(o);
		}
		System.out.println(olist.size());
		if(olist!=null){
			return oid;
		}
		
		return null;
	}

	/*
	 * @Method:loadOrdersByOid
	 * @Function:find orders according to orderid
	 * @param:String oid
	 * @author:wendy
	 * @time:2017-08-13
	 * 
	 */
	@Override
	@Transactional
	public List<Orders> loadOrdersByOid(String oid) {
		// TODO Auto-generated method stub
		List<Orders> olist=new ArrayList<Orders>();
		olist=ordersDao.findOrderByOid(oid);
		return olist;
	}

	/*
	 * @Method:loadDeliverInfoByOid
	 * @Function:find deliver info according to order id
	 * @param:String oid
	 * @author:wendy
	 * @time:2017-08-13
	 * 
	 */
	@Override
	@Transactional
	public DeliverInfo loadDeliverInfoByOid(String oid) {
		// TODO Auto-generated method stub
		DeliverInfo deliverInfo=new DeliverInfo();
		deliverInfo=(DeliverInfo) ordersDao.findOrderByOid(oid);
		return deliverInfo;
	}

	/*
	 * @Method:loadDeliverInfoByCid
	 * @Function:find deliver info according to client id
	 * @param:String cid
	 * @author:wendy
	 * @time:2017-08-13
	 * 
	 */
	@Override
	@Transactional
	public List<Orders> loadOrdersByCid(String cid) {
		// TODO Auto-generated method stub
		List<Orders> olist=new ArrayList<Orders>();
		olist=ordersDao.findOrdersByCid(cid);
		return olist;
	}

	@Override
	@Transactional
	public List<Orders> getAlls() {
	return baseDao.findLists(Orders.class);
	}

	@Override
	@Transactional
	public List<Orders> getByStatus(String state) {
	return ordersDao.findOrdersByOstate(state);
	}

	/*
	 * @Method:updateOrderStatus
	 * @Function:update order status by oid and status, for update oid state
	 * @param:String oid, String ostate
	 * @author:wendy
	 * @time:2017-08-15
	 * 
	 */
	@Override
	@Transactional
	public List<Orders> updateOrderStatus(String oid, String ostate) {
		// TODO Auto-generated method stub
		List<Orders> olist=new ArrayList<Orders>();
		olist=ordersDao.findOrderByOid(oid);
		for(Orders order:olist){
			order.setOstate(ostate);
			baseDao.persist(order);
		}
		return olist;
	}

	/*
	 * @Method:loadOrderCenter
	 * @Function:find order list by cid
	 * @param:String cid
	 * @author:weir
	 * @time:2017-08-15
	 * 
	 */
	@Override
	public List<OrdersVo> loadOrderCenter(String cid) {
		List<OrdersVo> results = new ArrayList<OrdersVo>();
		List<String> oidlists = ordersDao.finddistinctOid(cid);
		for(String oid : oidlists){
			Orders one = ordersDao.findOrderByOid(oid).get(0);
			OrdersVo vo = new OrdersVo();
			vo.setMname(one.getMerchant().getMname());
			vo.setOid(one.getOid());
			vo.setOstate(one.getOstate());
			vo.setOsum(one.getOsum());
			vo.setTime(one.getOtime());
			results.add(vo);
		}
		System.out.println(results.size());
		return results;
	}

	/*
	 * @Method:loadOrderCenterBystatus
	 * @Function:find order list by status
	 * @param:String status,String cid
	 * @author:weir
	 * @time:2017-08-15
	 * 
	 */
	@Override
	public List<OrdersVo> loadOrderCenterBystatus(String status,String cid) {
		List<OrdersVo> results = new ArrayList<OrdersVo>();
		List<String> oidlists = ordersDao.finddistinctOidWithState(status, cid);
		System.out.println(oidlists.size());
		for(String oid : oidlists){
			Orders one = ordersDao.findOrderByOid(oid).get(0);
			OrdersVo vo = new OrdersVo();
			vo.setMname(one.getMerchant().getMname());
			vo.setOid(one.getOid());
			vo.setOstate(one.getOstate());
			vo.setOsum(one.getOsum());
			vo.setTime(one.getOtime());
			results.add(vo);
		}
		return results;
	}

	/*
	 * @Method:updateComplaintStatusByOid
	 * @Function:update the complaint status by oid
	 * @param:String oid
	 * @author:wendy
	 * @time:2017-08-15
	 * 
	 */
	@Override
	@Transactional
	public List<Orders> updateComplaintStatusByOid(String oid) {
		// TODO Auto-generated method stub
		List<Orders> olist=new ArrayList<Orders>();
		olist=ordersDao.findOrderByOid(oid);
		for(Orders order:olist){
			order.setCompstate("1");
			baseDao.update(order);
		}
		return olist;
	}

	@Transactional
	@Override
	public void update(Orders order) {
	baseDao.update(order);
	}
	
}
